package com.sanqing.iframe;

import com.sanqing.dao.Dao;
import com.sanqing.main.ComBoxMyModel;
import com.sanqing.main.MyDocument;
import com.sanqing.model.BookInfo;
import com.sanqing.model.BookType;
import com.sanqing.model.Operater;
import sun.swing.table.DefaultTableCellHeaderRenderer;

import java.sql.*;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Enumeration;

public class AdminSelectFrame extends JInternalFrame {
    JTable table;//用于保存数据库信息
    JScrollPane scrollPane;//滚动容器
    Object[][] data;//数据信息
    Object[] title;//表格标题
    ResultSet resultSet;//存储sql语句结果


    public AdminSelectFrame() {
        super();
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            e.printStackTrace();
        }
        setFrameIcon(new ImageIcon("images/icon.png"));


        final BorderLayout borderLayout = new BorderLayout();           //边框布局管理器
        getContentPane().setLayout(borderLayout);           //设置布局
        setIconifiable(true);               //设置窗体可最小化
        setClosable(true);                  //设置窗体可关闭
        setMaximizable(true);

        setBounds(100, 100, 400, 350); //设置窗体位置和大小
        final JPanel mainPanel = new JPanel();          //创建中心面板
        mainPanel.setBorder(new EmptyBorder(5, 0, 170, 0));    //设置边框
        final GridLayout gridLayout = new GridLayout(2, 6);  //创建表格布局管理器
        gridLayout.setVgap(2);          //设置组件之间的垂直距离
        gridLayout.setHgap(2);          //设置组件之间的平行距离
        mainPanel.setLayout(gridLayout);
        getContentPane().add(mainPanel);

        /*
        * 建立表格
        * */
        title = new Object[]{"管理员编号","管理员姓名","级别"};
        resultSet = Dao.executeQuery("select * from tb_operator");

        try{
            resultSet.last();
            int row = resultSet.getRow();
            data = new Object[row][3];
            resultSet.first();
            resultSet.previous();
            int index = 0;
            while(resultSet.next()){
                data[index][0] = resultSet.getString("operator_id");
                data[index][1] = resultSet.getString("name");
                data[index][2] = resultSet.getString("admin");
                index++;
            }
        }catch (SQLException e){}

        table = new JTable(data,title);
        table.setRowHeight(25);//设置行高
        table.setFont(new Font("宋体", Font.PLAIN, 20));//行字体大小
        table.getTableHeader().setFont(new Font("宋体", Font.PLAIN, 20));//表头字体大小
        FitTableColumns(table);// 表格自适应列宽
        DefaultTableCellHeaderRenderer thr = new DefaultTableCellHeaderRenderer();//表格内容剧中
        thr.setHorizontalAlignment(JLabel.CENTER);
        table.getTableHeader().setDefaultRenderer(thr);
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();
        tcr.setHorizontalAlignment(JLabel.CENTER);
        table.setDefaultRenderer(Object.class, tcr);

        scrollPane = new JScrollPane(table);
        getContentPane().add(scrollPane,BorderLayout.CENTER);
    }
    public void FitTableColumns(JTable myTable) {
        JTableHeader header = myTable.getTableHeader();
        int rowCount = myTable.getRowCount();
        Enumeration columns = myTable.getColumnModel().getColumns();
        while (columns.hasMoreElements()) {
            TableColumn column = (TableColumn) columns.nextElement();
            int col = header.getColumnModel().getColumnIndex(column.getIdentifier());
            int width = (int) myTable.getTableHeader().getDefaultRenderer()
                    .getTableCellRendererComponent(myTable, column.getIdentifier()
                            , false, false, -1, col).getPreferredSize().getWidth();
            for (int row = 0; row < rowCount; row++) {
                int preferedWidth = (int) myTable.getCellRenderer(row, col).getTableCellRendererComponent(myTable,
                        myTable.getValueAt(row, col), false, false, row, col).getPreferredSize().getWidth();
                width = Math.max(width, preferedWidth);
            }
            header.setResizingColumn(column); // 此行很重要
            column.setWidth(width + myTable.getIntercellSpacing().width);
        }
    }
}
